//
//  weatherHandle.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "weatherHandle.h"
#import "hgcollectionView.h"

static weatherHandle *wheather;

@implementation weatherHandle

+ (weatherHandle *)shareWetherHandle{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        wheather= [[weatherHandle alloc] init];
    });
    return wheather;
}
- (void)relodeData{
    NSString *cityString = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
    if (cityString == nil) {
        return;
    }else if([cityString rangeOfString:@"市"].location<100){
        cityString=[cityString stringByReplacingOccurrencesOfString:@"市" withString:@""];
        self.placename = cityString;
    }
    else{
        self.placename =  cityString;
    }
    NSString * strr = [self.placename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *str = [NSString stringWithFormat:kUrl,strr];
    [LORequestManger GET:str success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *diction = dic[@"result"][@"data"][@"life"][@"info"];
        self.mod = [[molde alloc] init];
        self.mod.info = diction;
        [self.mod setValuesForKeysWithDictionary:diction];
       // NSLog(@"%@",[diction allKeys]);
      self.dateTime = dic[@"result"][@"data"][@"pm25"][@"dateTime"];
//        [self.dateTime writeToFile:[self filePathWithName:@"date.text"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",[self filePathWithName:@"date.text"]);
        self.mod.dateTime = self.dateTime;
        NSLog(@"%@",self.dateTime);
        self.weatherModle = [[molde alloc] init];
        NSMutableArray *dictionary = dic[@"result"][@"data"][@"weather"];
        self.weatherModle.weather= dic[@"result"][@"data"][@"weather"];
      
        [dictionary writeToFile:[self filePathWithName:@"11.text"] atomically:YES ];
        NSLog(@"%@",[self filePathWithName:@"11.text"]);
        NSArray * arr = [NSArray arrayWithContentsOfFile:[self filePathWithName:@"11.text"] ];
        NSLog(@"%@",arr);
        self.arr = [[NSMutableArray alloc] init];
        
             for (NSDictionary *dictint in dictionary) {
                 
                 molde *mode = [[molde alloc] init];

            [mode setValuesForKeysWithDictionary:dictint];
                 [self.arr addObject:mode];
         
            NSLog(@"%@", self.mood.date);
                 NSDictionary *dictiong = dic[@"result"][@"data"][@"pm25"][@"pm25"];
                     self.modlle = [molde setMode:dictiong];
                  NSLog(@"%@ %@",self.modlle.pm25,self.modlle.quality);
                 
                 
        }
      
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"relode" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"data" object:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
- (NSString *)filePathWithName:(NSString *)fileName {
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(9, 1, 1) lastObject];
    NSLog(@"%@",docPath);
    NSString *filePth = [docPath stringByAppendingPathComponent:fileName];
    return filePth;
    
    
}

- (NSString*)getWeather:(NSString *)weather andIndex:(NSInteger)index{
    NSArray *weeklist=[NSArray arrayWithObjects:@"等级",@"天气",@"温度",@"风向",@"风力",@"日落时间",nil];
    NSUInteger weekIndex=[weeklist indexOfObject:weather];
    return [weeklist objectAtIndex:(weekIndex+index)%7];
}
-(MBProgressHUD *) showHud:(id<MBProgressHUDDelegate>) delegate title:(NSString *) title  selector:(SEL) selector arg:(id) arg  targetView:(UIView *)targetView
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:targetView];
    [targetView addSubview:hud];
    hud.removeFromSuperViewOnHide = YES;
    hud.delegate = delegate;
    hud.labelText = title;
    [hud showWhileExecuting:selector
                   onTarget:delegate
                 withObject:arg
                   animated:YES];
    return hud;
}
- (void)cityName{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area"ofType:@"json"];
    //转化成NSData
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];
     self.array = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        self.array =  dic[@"cityList"];
        for (NSString *city in self.array) {
            [self.brr addObject:city];
        }
     }
}
-(NSMutableArray *)brr{
    if (!_brr) {
        _brr = [[NSMutableArray alloc] init];
    }
    return _brr;
}
- (void)p_removeAlertView:(UIAlertView *)alertView
{
    //alertView消失
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    
}
- (void)pleasedLoded:(UIViewController *)contrller{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    
    snsPlatform.loginClickHandler(contrller, [UMSocialControllerService defaultControllerService], YES, ^(UMSocialResponseEntity *response) {
        
        NSLog(@"response is %@", response);
        
        // 如果是授权到新浪微博，SSO之后如果想获取用户的昵称、头像等需要再获取一次账户信息
        
        [[UMSocialDataService defaultDataService]requestSocialAccountWithCompletion:^(UMSocialResponseEntity *response) {
            
            // 打印用户昵称
            
            NSLog(@"SinaWeibo's user name is %@", [[[response.data objectForKey:@"accounts"]objectForKey:UMShareToTencent] objectForKey:@"icon"]);
            NSURL *url = [[[response.data objectForKey:@"accounts"]objectForKey:UMShareToTencent] objectForKey:@"icon"];
            
            [[NSUserDefaults standardUserDefaults] setObject:url forKey:@"url"];
            
            
            NSString *name = [[[response.data objectForKey:@"accounts"]objectForKey:UMShareToTencent]objectForKey:@"username"];
            [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"name"];
            
        }];
        
    });
}
- (NSString *)setWeatherImageName:(NSString*)weather {
    if ([weather isEqualToString:@"阵雨"]||[weather isEqualToString:@"暴雨"]||[weather isEqualToString:@"大雨"]||[weather isEqualToString:@"中雨"]||[weather isEqualToString:@"小雨"]||[weather isEqualToString:@"大到暴雨"]) {
        NSString *name = @"lightrain@2x";
        return name;
    }
    else if ([weather isEqualToString:@"多云"]||[weather isEqualToString:@"阴"]){
        NSString *name = @"cloudy";
        return name;
        
    }else if ([weather isEqualToString:@"雷阵雨"]){
        NSString *name = @"storm@2x";
        return name;
    }else if ([weather isEqualToString:@"晴转多云"]||[weather isEqualToString:@"多云转晴"]){
        NSString *name = @"partlysunny@2x";
        return name;
    }else if ([weather isEqualToString:@"晴"]){
        NSString *name = @"clear";
        return name;
    }else if ([weather rangeOfString:@"雪"].length>0){
        NSString *name = @"flurries";
        return name;
    }
    return @"打发的";
    
}



@end
